package com.jin.blog.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.kit.StringKit;
import com.jin.blog.core.BlogConstants;
import com.jin.blog.email.Email;
import com.jin.blog.lucene.ArticleLuceneService;
import com.jin.blog.pojo.Article;
import com.jin.blog.pojo.Message;

public class IndexController extends BaseController {
	public void index() {
		final  Integer pageNum = getParaToInt("p", 1);
		Page<Article> page_news = Article.dao.paginateByCache("article", "page_news_" + pageNum, pageNum, BlogConstants.PAGE_SIZE, 
				"select id,title,viewCount,replyCount,createDateTime,content,tags,type,projectId", 
				" from article where finish = 1 order by id desc");
		setAttr("page_news", page_news);
		getCommonData();
		
		render("index.html");
	}
	public void search(){
		String keyword = getPara("q");
		Integer pageNo = getParaToInt("p", 1);
		Page<Article> page = null;
		if(!StringKit.isBlank(keyword)){
			page = ArticleLuceneService.me().query(keyword, BlogConstants.PAGE_SIZE, pageNo);
			setAttr("q", keyword);
			setAttr("page_news", page);
			getCommonData();
			render("index.html");
		}else{
			index();
		}
	}
	
	public void archives(){
		setAttr("yearList", Db.find("select year(createDateTime) 'year' from article group by year(createDateTime) order by year(createDateTime) desc"));
		setAttr("monthList", Db.find("select year(createDateTime) 'year',month(createDateTime) 'month' from article group by year(createDateTime),month(createDateTime) order by month(createDateTime) desc"));
		setAttr("titleList", Db.find("select year(createDateTime) 'year',month(createDateTime) 'month',day(createDateTime) 'day',id,title from article order by createDateTime desc"));
		getCommonData();
		List<Map<String, Object>>crumbs = new ArrayList<>();
		Map<String, Object>map = new HashMap<>();
		map.put("name", "归档");
		crumbs.add(map);
		setAttr("crumbs", crumbs);
		render("archives.html");
	}
	
	public void me(){
		getCommonData();
		List<Map<String, Object>>crumbs = new ArrayList<>();
		Map<String, Object>map = new HashMap<>();
		map.put("name", "关于我");
		crumbs.add(map);
		setAttr("crumbs", crumbs);
		render("me.html");
	}

	public void addMsg(){
		JSONObject json = new JSONObject();
		Message message = getModel(Message.class);
		if(Pattern.matches("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$", message.getStr("email"))){
			message.set("dateTime", new Date());
			message.save();
			Email _email = new Email();
			_email.setContent("<strong>" + message.getStr("nick") + "("+message.getStr("email")+"):</strong>" + message.getStr("content"));
			_email.send();
			json.put("success", true);
			json.put("msg", "提交成功");
		}else{
			json.put("success", false);
			json.put("msg", "请输入有效邮箱地址");
		}
		renderJson(json.toJSONString());
	}
}
